package com.demo.game1;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GameView extends SurfaceView {

	private Bitmap bmp;
	private Bitmap bmpBall;
	private Bitmap bmpBlood;
    private SurfaceHolder holder;
    private GameThread gamethread;
    private List<TempSprite> temps = new ArrayList<TempSprite>();
    private Evil evil;
    private Ball ball;
    private Paint paint;
    private Paint score;
    Context mcontext;
	
	public GameView(Context context) {
		super(context);
		
		mcontext = context;
		gamethread = new GameThread(this);
		holder = getHolder();
		holder.addCallback(new Callback() {
			
			public void surfaceDestroyed(SurfaceHolder holder) {
				
				 boolean retry = true;
				 gamethread.setRunning(false);
                 while (retry) {
                        try {
                        	gamethread.join();
                              retry = false;
                        } catch (InterruptedException e) {
                        }
                 }
				
			}
			
			public void surfaceCreated(SurfaceHolder holder) {
				createEvil();
				createBall();
				gamethread.setRunning(true);
				gamethread.start();
				
			}
			
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				// TODO Auto-generated method stub
				
			}
		});
						
	}
	
	public void createEvil()
	{
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.evil);
		evil = new Evil(this, bmp);
	}
	
	public void createBall()
	{
		bmpBall = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
		ball = new Ball(this, bmpBall);
	}
	
	public void checkCollison()
	{
		 if(ball.y + bmpBall.getHeight() >= this.getHeight()|| evil.isCollison(ball.x, ball.y, bmpBall.getWidth(), bmpBall.getHeight()))
         {
	       	  if(ball.y + bmpBall.getHeight() >= this.getHeight())
	       	  {
	       		  Constrant.miss +=1;
	       		  if(Constrant.miss == 3)
	       		  {
	       			  goGameOver();
	       		  }
	       	  }
	       	  else if(evil.isCollison(ball.x, ball.y, bmpBall.getWidth(), bmpBall.getHeight()))
	       	  {
	       		  Constrant.score += 1;
	       		  if(Constrant.score > 0 && Constrant.score%3==0)
	       		  {
	       			goNextLevel();
	       		  }
	       	  }
	       		  
		       	  ball.isDraw = false;
		       	  ball.y = 0;
	         }
	}
	
	
	public void goNextLevel()
	{
		Constrant.level +=1;
		Constrant.miss = 0;
		if(evil.xspeed<0)
		{
			evil.xspeed += -5;
		}
		else
		{
			evil.xspeed += 5;
		}
				
	}
	
	public void goGameOver()
	{ 
		Constrant.isOver = true;
		gamethread.setRunning(false);	
		restartGame();
	}
	
	public void restartGame()
	{
		evil.x = 0;
		evil.currentRowFrame = 2;
		evil.xspeed = 5;
		Constrant.score = 0;
		Constrant.miss = 0;
		Constrant.level = 1;
		gamethread.setRunning(true);		
	}
	
	
	 @Override
     protected void onDraw(Canvas canvas) {
		 
		 canvas.drawColor(Color.BLACK);    		 
          evil.onDraw(canvas);          
          ball.onDraw(canvas);
          checkCollison();
          drawPanel(canvas);
     }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		int xtouch ;
		int ytouch ;
		int action = event.getAction();
		
		switch (action) {
		case MotionEvent.ACTION_UP:
			
				xtouch = (int)event.getX();
				ball.x = xtouch;
				ball.isDraw = true;
						
			break;

		default:
			break;
		}
		
		
		return true;
	}

	 
	 
	public void drawPanel(Canvas canvas)
	{
		 paint = new Paint();
		 paint.setColor(Color.WHITE);
		 paint.setStyle(Style.FILL_AND_STROKE);
		 paint.setStrokeWidth(1);
		 canvas.drawLine(0, 30, this.getWidth(), 30, paint);
		 
		 
		 paint.setColor(Color.WHITE);
		 paint.setStyle(Paint.Style.FILL);
		 paint.setAntiAlias(true);
		 paint.setTextSize(20);
		 paint.setStrokeWidth(3);
		 canvas.drawText("Score : " +Constrant.score, 0, 20, paint);
         
		 paint.setColor(Color.WHITE);
		 paint.setStyle(Paint.Style.FILL);
		 paint.setAntiAlias(true);
		 paint.setTextSize(20);
		 paint.setStrokeWidth(3);
		 canvas.drawText("Miss : " +Constrant.miss, this.getWidth() - 80 , 20 , paint);
		 
		 paint.setColor(Color.WHITE);
		 paint.setStyle(Paint.Style.FILL);
		 paint.setAntiAlias(true);
		 paint.setTextSize(20);
		 paint.setStrokeWidth(3);
		 canvas.drawText("Level : " + Constrant.level, this.getWidth()/2 -40 , 20, paint);
	}
	 
}
